package com.hyt.it.ogt.kq.main.controller.bm;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.core.util.FastJsonUtil;
import com.hyt.exception.annotation.ThrowsAdvice;
import com.hyt.it.ogt.kq.common.bm.utils.Constant;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.service.bm.model.entity.Project;
import com.hyt.it.ogt.kq.service.bm.model.param.MyExamRecordParam;
import com.hyt.it.ogt.kq.service.bm.model.vo.*;
import com.hyt.it.ogt.kq.service.bm.service.IProjectService;
import com.hyt.it.ogt.kq.service.bm.service.IStudentInfoService;
import com.hyt.it.ogt.kq.service.bm.service.IStudentProjectService;
import com.hyt.it.ogt.kq.service.model.vo.wechat.WeChatCandidateArrangeDetailVO;
import com.hyt.it.ogt.kq.service.model.vo.wechat.WeChatCandidateArrangeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 考生报名表
 * </p>
 *
 * @author wangt
 * @since 2020-05-15
 */
@Slf4j
@RestController
@Api(tags = "9.考生报名关联信息接口", value = "考生报名关联信息接口")
@ApiSort(value = 9)
@RequestMapping("/bm/student-project")
public class StudentProjectController extends BaseController {

    @Resource
    private IStudentInfoService iStudentInfoService;
    @Resource
    private IStudentProjectService iStudentProjectService;
    @Resource
    private IProjectService projectService;


    @ApiOperation(value = "9.1.1 查看考生报名科目页",
            notes = "20000:成功；15004：未查找到考生报名数据",
            response = StudentProjectInfoVo.class)
    @ApiOperationSort(value = 1)
    @ThrowsAdvice(code = 31010176, print = "未查找到考生报名数据")
    @GetMapping(value = "/query/{projectId}/{regId}")
    public ApiResponse<Object> query(@PathVariable String projectId, @PathVariable String regId) {
        StudentProjectInfoVo studentInfo;
        studentInfo = iStudentInfoService.getProjectSubjectById(projectId, regId);
        if (studentInfo != null) {
            if (null != studentInfo.getStudentSubjectList()) {
                for (ProjectSubjectVo vo : studentInfo.getStudentSubjectList()) {
                    if (null != vo.getIsPay() && 0 == vo.getIsPay()) {
                        vo.setPrice(null);
                    }
                }
            }
            return ApiResponse.builder()
                    .code(Constant.RESPONSE_CODE_SUCCESS)
                    .data(studentInfo)
                    .build();
        } else {
            return ApiResponse.builder()
                    .code(Constant.RESPONSE_CODE_31010176)
                    .data(studentInfo)
                    .build();
        }
    }

    @ApiOperation(value = "9.1.2 查询考生已报名项目", notes = "20000:成功")
    @ApiOperationSort(value = 2)
    @GetMapping(value = "/queryStudentProject")
    public ApiResponse<Object> queryStudentProject(String regId) {
        try {
            List<StudentProjectVo> list = iStudentInfoService.queryStudentProject(regId);
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(list).build();
        } catch (Exception ex) {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_31010136).build();
        }
    }

    @ApiOperation(value = "9.1.3 工作台统计小时或日期报名数", notes = "20000:成功;31010136:查询异常")
    @ApiOperationSort(value = 3)
    @GetMapping(value = "/statStudentByDate")
    public ApiResponse<Object> statStudentByDate(ProjectParam param) {
        log.info("## 工作台统计小时或日期报名数参数{}", FastJsonUtil.getBeanToJson(param));
        try {
            param.setOfficeId(getOfficeId());
            param.setUserId(getUserId());
            List<StudentStatVo> list = iStudentProjectService.statStudentByHour(param);
            return ApiResponse.builder()
                    .code(Constant.RESPONSE_CODE_SUCCESS)
                    .data(list)
                    .build();
        } catch (Exception e) {
            log.error("# 工作台统计小时或日期报名数异常", e);
            return ApiResponse.builder()
                    .code(Constant.RESPONSE_CODE_31010136)
                    .build();
        }
    }


    @ApiOperation(value = "9.1.4 获取考生考试安排列表", response = WeChatCandidateArrangeVO.class)
    @ApiOperationSort(value = 4)
    @GetMapping(value = "/getWeChatCandidateArrange")
    public ApiResponse<Object> getWeChatCandidateArrangeVO(@RequestParam(value = "officeId") String officeId,
                                                           @RequestParam(value = "regId") String regId) {
        try {
            List<WeChatCandidateArrangeVO> WeChatCandidateArrangeVOs = new ArrayList<>();
            List<WeChatCandidateArrangeVO> list = iStudentProjectService.getWeChatCandidateArrangeVO(officeId, regId);
            if (CollUtil.isNotEmpty(list)) {
                WeChatCandidateArrangeVOs = BeanUtil.copyToList(list, WeChatCandidateArrangeVO.class);
            }
            // 补充准考证下载时间
            if (CollUtil.isNotEmpty(WeChatCandidateArrangeVOs)) {
                for (WeChatCandidateArrangeVO WeChatCandidateArrangeVO : WeChatCandidateArrangeVOs) {
                    String bmProjectId = WeChatCandidateArrangeVO.getSignUpId();
                    Project project = projectService.getById(bmProjectId);
                    if (null != project) {
                        WeChatCandidateArrangeVO.setAdmissionBeginTime(project.getAdmissionBeginTime());
                        WeChatCandidateArrangeVO.setAdmissionEndTime(project.getAdmissionEndTime());
                    }
                }
            }
            return ApiResponse.builder()
                    .code(Constant.RESPONSE_CODE_SUCCESS)
                    .data(WeChatCandidateArrangeVOs)
                    .build();
        } catch (Exception e) {
            log.error("# 获取考生考试安排列表异常", e);
            return ApiResponse.builder()
                    .code(Constant.RESPONSE_CODE_31010111)
                    .build();
        }
    }


    @ApiOperation(value = "9.1.5 获取考生考试安排详情", response = WeChatCandidateArrangeDetailVO.class)
    @ApiOperationSort(value = 5)
    @GetMapping(value = "/getWeChatCandidateArrangeDetail")
    public ApiResponse<Object> getWeChatCandidateArrangeDetailVO(@RequestParam(value = "projectId") String projectId, @RequestParam(value = "timeId") String timeId, @RequestParam(value = "regId") String regId) {
        try {
            WeChatCandidateArrangeDetailVO detail = iStudentProjectService.getWeChatCandidateArrangeDetailVO(projectId, timeId, regId);
            return ApiResponse.builder()
                    .code(Constant.RESPONSE_CODE_SUCCESS)
                    .data(detail)
                    .build();
        } catch (Exception e) {
            log.error("# 获取考生考试安排详情异常", e);
            return ApiResponse.builder()
                    .code(Constant.RESPONSE_CODE_31010111)
                    .build();
        }
    }

    @ApiOperation(value = "9.1.6 我的报名", notes = "20000:成功,31010136:查询失败，异常", response = ProjectStatVo.class, responseContainer = "List")
    @ApiOperationSort(value = 6)
    @ThrowsAdvice(code = 31010136, print = "我的报名异常")
    @GetMapping(value = "/getMyProject")
    public ApiResponse<List<ProjectStatVo>> getMyProject(@RequestParam String officeId, @RequestParam String regId) throws Exception {
        return ApiResponse.<List<ProjectStatVo>>builder().code(Constant.RESPONSE_CODE_SUCCESS).data(iStudentProjectService.getMyProject(officeId, regId)).build();
    }

    @ApiOperation(value = "9.2.1 我的考试记录", notes = "20000:成功,31092101：查询我的考试记录异常；31092102：获取考生考试安排失败；31092103：考生考试信息获取失败；")
    @ApiOperationSort(value = 7)
    @ThrowsAdvice(code = 31092101, print = "查询我的考试记录异常")
    @GetMapping(value = "/myExamRecord")
    public ApiResponse<List<StudentExamRecordVo>> myExamRecord(MyExamRecordParam param) {
        Optional.ofNullable(this.getUserId())
                .ifPresent(param::setRegId);
        return ApiResponse.<List<StudentExamRecordVo>>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(iStudentProjectService.myExamRecord(param))
                .build();
    }
}
